package com.yangfan.util;

import java.util.HashMap;
import java.util.Map;

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;

public class OCRutil {
	public static void main(String[] args) throws Exception {
		//Map<String, String> ck = new HashMap<String, String>();
		//ck.put("clwz_blc_atc", "u0CDoCUCu0PMKx6bQWXkpYjLEylRDnpDQYaEKwkTJZA=");
        //String url = "https://login.10086.cn/captchazh.htm?type=10";
        //Response content = SpiderJsoupUtil.getContent(url, null, ck);
        //
        //System.out.println(content.body());

        String str = doGs("山西省", "大数据");

        System.out.println(str);

        //
        //for(int i=0;i<1;i++){
		//	String url = "https://login.10086.cn/captchazh.htm?type=10";
		//	Response content = SpiderJsoupUtil.getContent(url, null, ck);
		//	long time1 = System.currentTimeMillis();
		//	String doocr = doocr(content.bodyAsBytes(), 28);
		//	long time2 = System.currentTimeMillis();
		//	System.out.println(time2-time1+":"+doocr);
		//	FileUtils.writeByteArrayToFile(new File("/Users/yp-tc-m-2641/Documents/"+doocr+".png"), content.bodyAsBytes());
		//}



	}
	public static String doocr(byte[] bs,int website) throws Exception{
		String url = "http://123.57.23.67/ocr";
		Map<String, String> data = new HashMap<String, String>();
		if(null!=bs){
			StringBuffer sb = new StringBuffer();
			for(int b : bs){
				sb.append(b);
				sb.append(",");
			}
			sb = sb.deleteCharAt(sb.length()-1);
			data.put("image", sb.toString());
		}else{
			data.put("image", "");
		}
		bs =null;
		data.put("website", ""+website);
		Response content = SpiderJsoupUtil.getContent(url, data, null,Method.POST,null);
		return content.body().equals("null")?null:content.body();
	}

    public static String doGs(String province, String kw) {
        String url = "http://123.57.23.67/gs";
        Map<String, String> data = new HashMap<String, String>();
        data.put("province", province);
        data.put("kw", kw);
        Response content = null;
        try {
            content = SpiderJsoupUtil.getContent(url, data, null, Method.POST,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        data = null;
		//System.out.println(content.body());
        System.gc();
        return content == null ? null : (content.body().equals("null") ? null : content.body());
    }
}
